package com.hbzjjzx.zuul.zuul;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: springcloudhbats
 * @description
 * pre：路由之前
 * routing：路由之时
 * post： 路由之后
 * error：发送错误调用
 * filterOrder：过滤的顺序
 * shouldFilter：这里可以写逻辑判断，是否要开启过滤
 * run：过滤器的具体逻辑。可用很复杂，包括查sql，nosql去判断该请求到底有没有权限访问。
 * @author: 周佳佳
 * @create: 2020-01-22 15:27
 **/
@Component
public class UserLoginFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext context=RequestContext.getCurrentContext();
        HttpServletRequest request=context.getRequest();
        HttpServletResponse response=context.getResponse();
        String appId=request.getParameter("appId");
        if(appId!=null &&"1".equals(appId)){
            return null;
        }else{
            context.setSendZuulResponse(false);//过滤掉该请求，不执行转发(路由)
            context.setResponseStatusCode(401);//设置响应码
            context.setResponseBody("qing lian xi yJJ");
            return null;
        }
    }
}
